{% extends 'name_generate/base_view.html' %}

{% block page_title %}
    {{ projectclass.name }}
    <input type="text" name="pc" style="display:none;" maxlength="30" id="id_pc" value="{{ projectclass.pk }}">
{% endblock %}


{% block form_com %}
    <form method="post"  id="TechnicalForm" data-schememodule-url="{% url 'ajax_load_scheme_module' %}" data-generate-url="{% url 'ajax_generate_tfname' %}" onsubmit="return commit()">
        {% csrf_token %}
          <label for="ProjectName">项目名称</label>
          {{ form.project }}
          <label for="SchemeName">方案名（选填）</label>
          {{ form.scheme }}
          <label for="ModuleName">模块名（选填）</label>
          {{ form.module }}
          <label>文件名称</label>
           {{ form.name }}
          <label>日期（选填）</label>
           {{ form.date }}
          <label>作者（选填）</label>
           {{ form.author }}

          <label>版本</label>
        <select name="version" id="version">
            <option value="debug" selected>评审</option>
            <option value="release" >发布</option>
        </select>
          <label></label>
          <input name="Creat" type="button" id="click" value="生成文件全名">
          <label></label>
          {{ form.result }}
          {{ form.version }}
          {{ form.projectflieclass }}
          {{ form.number }}
          <label></label>
          <label></label>
          <input class="btn btn-danger"  type="submit" value="确认使用文件编号"/>
        </form>
      <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
      <script  type="text/javascript">
          var ddd = new Date();
          var day =ddd.getDate();
          var month = ddd.getMonth()+1; 
          if(ddd.getMonth() < 9){
                month = "0"+(ddd.getMonth()+1); 
          }
          if(ddd.getDate()<10){
               day = "0"+ddd.getDate(); 
          }
          var datew = ddd.getFullYear()+"-"+month+"-"+day;
          var datew = datew.toString();

          $("#id_date").val(datew);

      function commit()
      {
         var result =  $("#id_result").val();

         if(result == '')
         {
             alert("请先点击生成文件编号");
             return false;
         }
          if(confirm("您确定使用该文件编号并将其存入数据库吗? 确认后将跳转到主页")){
             alert("该命名已提交到数据库");
          }
          else{
              alert("您取消了提交");
              return false;
          }
          return true;

      }

        $("#id_project").change(function () {
          var url = $("#TechnicalForm").attr("data-schememodule-url");
          var id_project = $(this).val();

          $.ajax({
            url: url,
            data: {
              'project': id_project
            },
            success: function (data) {
                var res = data.split('splite')
                $("#id_scheme").html(res[0]);
                $("#id_module").html(res[1]);
            }
          });
        });

        $("#click").click(function () {
             var url = $("#TechnicalForm").attr("data-generate-url");
             var id_project =  $("#id_project").val();
             $("#id_projectflieclass").val($("#id_pc").val());
             var id_projectflieclass =  $("#id_projectflieclass").val();
             var id_scheme =  $("#id_scheme").val();
             var id_module =  $("#id_module").val();
             var id_name =  $("#id_name").val();
             var id_date =  $("#id_date").val();
             var id_author =  $("#id_author").val();
             var version =  $("#version").val();
             if (id_project == ''){
                 window.alert("请选择项目");
                 return;
             }

             if (id_name == '请输入文件名称'||id_name == ''){
                 window.alert("请输入文件名称");
                 return;
             }

          $.ajax({
            url: url,
            data: {
                'project': id_project,
                'projectflieclass':id_projectflieclass,
                'scheme': id_scheme,
                'module': id_module,
                'name': id_name,
                'date': id_date,
                'author': id_author,
                'version':version,
            },
            success: function (data) {
                var obj = JSON.parse(data);
                $("#id_result").val(obj[0].result)
                $("#id_version").val(obj[0].version)
                $("#id_number").val(obj[0].number)
            }
          });
        });

      </script>
{% endblock %}